package com.sensetime.jv.controller;

import com.github.pagehelper.PageInfo;
import com.sensetime.jv.annotation.RequirePermission;
import com.sensetime.jv.entity.PermissionsJv;
import com.sensetime.jv.entity.ReturnWrapper;
import com.sensetime.jv.service.PermissionService;
import com.sensetime.jv.vo.request.PermissionListReqVO;
import com.sensetime.jv.vo.request.PermissionSaveReqVO;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/permission")
@Slf4j
@RequiredArgsConstructor
public class PermissionController {

    private final PermissionService permissionService;

    /* ---------- 权限表 CRUD ---------- */

    /** 列表 + 分页 */
    @GetMapping("/list")
    @RequirePermission(description = "获取权限列表")
    public ReturnWrapper<PageInfo<PermissionsJv>> list(PermissionListReqVO vo) {
        return ReturnWrapper.success(permissionService.listPermissions(vo));
    }

    /** 详情 */
    @GetMapping("/{id}")
    @RequirePermission(description = "获取权限详情")
    public ReturnWrapper<PermissionsJv> detail(@PathVariable Integer id) {
        return ReturnWrapper.success(permissionService.getPermissionById(id));
    }

    /** 新增/更新 */
    @PostMapping
    @RequirePermission(description = "插入或者更新权限")
    public ReturnWrapper<Integer> save(@Valid @RequestBody PermissionSaveReqVO vo) {
        return ReturnWrapper.success(permissionService.savePermission(vo));
    }

    /** 删除（级联解绑角色 & URL） */
    @DeleteMapping("/{id}")
    @RequirePermission(description = "删除权限")
    public ReturnWrapper<Void> delete(@PathVariable Integer id) {
        permissionService.deletePermission(id);
        return ReturnWrapper.success();
    }

}